package com.qiaofang.service.impl;

import com.github.wenhao.jpa.Specifications;
import com.google.common.collect.Lists;
import com.qiaofang.common.constant.*;
import com.qiaofang.common.exception.BusinessException;
import com.qiaofang.common.exception.SystemException;
import com.qiaofang.common.model.DevPremisesExport;
import com.qiaofang.common.model.PageDTO;
import com.qiaofang.common.model.UiListBean;
import com.qiaofang.common.utils.DateUtil;
import com.qiaofang.common.utils.PageUtil;
import com.qiaofang.common.utils.RedisUtil;
import com.qiaofang.common.utils.StringUtil;
import com.qiaofang.constant.CompanyTypeEnum;
import com.qiaofang.constant.LogoTypeEnum;
import com.qiaofang.constant.OperationTypeEnum;
import com.qiaofang.constant.StatusEnum;
import com.qiaofang.dto.*;
import com.qiaofang.entity.Company;
import com.qiaofang.entity.Logo;
import com.qiaofang.entity.PartnerInfo;
import com.qiaofang.mapper.DevCompanyExportMapper;
import com.qiaofang.repository.CompanyRepository;
import com.qiaofang.repository.LogoRepository;
import com.qiaofang.service.*;
import com.qiaofang.vo.CompanyVO;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
public class MerchantCompanyServiceImpl extends CompanyServiceImpl implements MerchantCompanyService {

    @Autowired
    private CompanyRepository companyRepository;

    @Autowired
    private PartnerInfoService partnerInfoService;

    @Autowired
    private DevCompanyExportMapper devCompanyExportMapper;

    @Autowired
    private WxConfigService wxConfigService;

    @Autowired
    private LogoRepository logoRepository;

    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private OperationLogService operationLogService;

    @Autowired
    private OtherCompanyService otherCompanyService;

    @Override
    public UiListBean listCompany(ListCompanyDTO dto, PageDTO page) {
        Specification<Company> specification = Specifications.<Company>and()
                .eq("companyType", CompanyTypeEnum.Developer.getType())
                .eq(StringUtil.isNotBlank(dto.getStatus()), "status", dto.getStatus())
                .eq(dto.getType() != null, "companyTypeAid", dto.getType())
                .eq(StringUtil.isNotBlank(dto.getOutsideSwitch()), "outsideSwitch", dto.getOutsideSwitch())
                .eq(dto.getProvince() != null, "province", dto.getProvince())
                .eq(dto.getCity() != null, "city", dto.getCity())
                .eq(dto.getZone() != null, "zone", dto.getZone())
                .between(StringUtil.isNotBlank(dto.getCreateTimeStart()), "createTime"
                        , DateUtil.minOfString(dto.getCreateTimeStart()),
                        DateUtil.maxOfString(dto.getCreateTimeEnd()))
                .eq(StringUtil.isNotBlank(dto.getMerchant2()) && IsLinkM2Enum.YES.getNo().equals(dto.getMerchant2())
                        , "merchant2", dto.getMerchant2())
                .eq(null != dto.getMerchant2() && !IsLinkM2Enum.YES.getNo().equals(dto.getMerchant2())
                        , "merchant2", null)
                .predicate(StringUtil.isNotBlank(dto.getSearchContent()),
                        Specifications.or()
                                .like("companyName", StringUtil.addLikeParam(dto.getSearchContent()))
                                .like("companyAbbreviation", StringUtil.addLikeParam(dto.getSearchContent()))
                                .like("companyUuid", StringUtil.addLikeParam(dto.getSearchContent()))
                                .like("domain", StringUtil.addLikeParam(dto.getSearchContent()))
                                .like("merchantCode", StringUtil.addLikeParam(dto.getSearchContent()))
                                .build())
                .eq("delFlag", DelFlagEnum.UNDEL.getNo())
                .build();
        Page<Company> companies = companyRepository.findAll(specification, PageUtil.getPageByDTO(page, PageUtil.getCreateTimeDesc()));
        UiListBean<CompanyVO> uiListBean = new UiListBean();
        uiListBean.setList(convertListDomain2VO(companies.getContent()));
        uiListBean.setTotalCount(companies.getTotalElements());
        return uiListBean;
    }

    private List<CompanyVO> convertListDomain2VO(List<Company> companies) {
        List<CompanyVO> vos = Lists.newArrayList();
        if (companies != null) {
            for (Company company : companies) {
                CompanyVO vo = convertDomain2VO(company, false);
                vos.add(vo);
            }
        }
        return vos;
    }

    private CompanyVO convertDomain2VO(Company company, boolean extend) {
        if (company == null) {
            return null;
        }
        CompanyVO vo = new CompanyVO();
        vo.setId(company.getId());
        vo.setCompanyUuid(company.getCompanyUuid());
        vo.setCompanyName(company.getCompanyName());
        vo.setCompanyAbbreviation(company.getCompanyAbbreviation());
        vo.setStatus(company.getStatus());
        if (CompanyTypeEnum.Developer.getType().equals(company.getCompanyType())) {
            if (company.getCompanyTypeAid() != null) {
                vo.setCompanyType(company.getCompanyTypeAid());
            } else {
                vo.setCompanyType(company.getCompanyType());
            }
        } else {
            vo.setCompanyType(company.getCompanyType());
        }
        vo.setProvince(company.getProvince());
        vo.setCity(company.getCity());
        vo.setZone(company.getZone());
        vo.setAddress(company.getAddress());
        vo.setDomain(company.getDomain());
        vo.setIntroduction(company.getIntroduction());
        vo.setMerchant2(company.getMerchant2());
        vo.setMerchantCode(company.getMerchantCode());
        vo.setOutsideSwitch(company.getOutsideSwitch());
        vo.setCheckSwitch(company.getCheckSwitch());
        vo.setCheckTemp(company.getCheckTemp());
        vo.setCommissionAuthorizationTime("-");
        if(1 == company.getEnableWeappMsg()) {
        	vo.setWeappMessage(true);
        }else {
        	vo.setWeappMessage(false);
        }
        
        if (StringUtil.equals(YesOrNoEnum.YES.getNo(), company.getCommissionFlag())) {
            vo.setCommission(true);
        } else {
            vo.setCommission(false);
        }
        vo.setMessageAuthorizationTime("-");
        if (StringUtil.equals(YesOrNoEnum.YES.getNo(), company.getMessageFlag())) {
            vo.setMessage(true);
        } else {
            vo.setMessage(false);
        }
        vo.setCreateTime(company.getCreateTime());
        vo.setNumberOfProperties(company.getNumberOfProperties());
        vo.setNumberOfDistributors(company.getNumberOfDistributors());
        if (extend) {
            //关联信息
            //vo.setPartnerInfo(partnerInfoService.getAdminPartnerInfoByCompanyId(company.getId()));
            vo.setWxConfigs(wxConfigService.getWxConfigs(company));
            getLogo(vo);
        }
        List<BindCompanyDTO> bindList = otherCompanyService.selectOtherCompanyList(company.getId());
        if(CollectionUtils.isEmpty(bindList)){
            vo.setBindOtherCompany(false);
        }else{
            vo.setBindOtherCompany(true);
        }
        vo.setCheckM2Repeat(company.getCheckM2Repeat());
        return vo;
    }

    private void getLogo(CompanyVO companyVO) {
        Logo example = new Logo();
        example.setLogoMerchantId(companyVO.getId());
        example.setDelFlag(DelFlagEnum.UNDEL.getNo());
        List<Logo> logos = logoRepository.findAll(Example.of(example));
        if (logos != null) {
            for (Logo logo : logos) {
                if (StringUtil.equals(logo.getLogoType(), LogoTypeEnum.TYPE_1.getNo())) {
                    companyVO.setH5LoginLogo(logo.getLogoUrl());
                }
                if (StringUtil.equals(logo.getLogoType(), LogoTypeEnum.TYPE_2.getNo())) {
                    companyVO.setAssistantLoginLogo(logo.getLogoUrl());
                }
                if (StringUtil.equals(logo.getLogoType(), LogoTypeEnum.TYPE_3.getNo())) {
                    companyVO.setAssistantIndexLogo(logo.getLogoUrl());
                }
                if (StringUtil.equals(logo.getLogoType(), LogoTypeEnum.TYPE_4.getNo())) {
                    companyVO.setAssistantExitLogo(logo.getLogoUrl());
                }
                if (StringUtil.equals(logo.getLogoType(), LogoTypeEnum.TYPE_5.getNo())) {
                    companyVO.setDevelopersLoginLogo(logo.getLogoUrl());
                }
                if (StringUtil.equals(logo.getLogoType(), LogoTypeEnum.TYPE_6.getNo())) {
                    companyVO.setDevelopersIndexLogo(logo.getLogoUrl());
                }
                if (StringUtil.equals(logo.getLogoType(), LogoTypeEnum.TYPE_7.getNo())) {
                    companyVO.setAgencyLoginLogo(logo.getLogoUrl());
                }
                if (StringUtil.equals(logo.getLogoType(), LogoTypeEnum.TYPE_8.getNo())) {
                    companyVO.setAgencyIndexLogo(logo.getLogoUrl());
                }
                if (StringUtil.equals(logo.getLogoType(), LogoTypeEnum.TYPE_9.getNo())) {
                    companyVO.setAgencyServiceLogo(logo.getLogoUrl());
                }
                if (StringUtil.equals(logo.getLogoType(), LogoTypeEnum.TYPE_10.getNo())) {
                    companyVO.setAgencyWeappLogo(logo.getLogoUrl());
                }
            }
        }
    }

    @Override
    public void addCompany(CompanyDTO dto) {
        Company company = saveCompany(dto);
        //保存微信配置
        wxConfigService.saveWxConfig(company, dto.getWxConfigs());
        //保存logo
        addLogo(company.getId(), dto);
        //部门
        departmentService.addTopDepartment(company);
        //保存管理员
//        PartnerInfoDTO partnerInfoDTO = dto.getPartnerInfo();
//        partnerInfoDTO.setCompanyId(String.valueOf(company.getId()));
//        partnerInfoDTO.setAccountType(PartnerInfoTypeEnum.ADMIN.getNo());
//        partnerInfoService.addPartnerInfo(partnerInfoDTO);
        //操作日志
        operationLogService.addOperationLog(OperationTypeEnum.ADD.getNo(), company.getId());
    }

    private Company saveCompany(CompanyDTO dto) {
        Company company;
        if (dto.getId() != null) {
            company = companyRepository.findById(dto.getId())
                    .orElseThrow(() -> new BusinessException(ExceptionEnum.NOT_FIND_DB_DATA));
            company.setDomain(dto.getDomain());
        } else {
            company = new Company();
            company.setDomain(dto.getDomain());
            company.setCompanyUuid(UUID.randomUUID().toString());
        }
        if (dto.getCompanyType() != null) {
            if (!CompanyTypeEnum.Developer.getType().equals(dto.getCompanyType())) {
                company.setCompanyType(CompanyTypeEnum.Developer.getType());
                company.setCompanyTypeAid(dto.getCompanyType());
            } else {
                company.setCompanyType(dto.getCompanyType());
            }
        } else {
            //开发商
            company.setCompanyType(CompanyTypeEnum.Developer.getType());
        }
        company.setStatus(StatusEnum.Enable.getNo());
        company.setCompanyName(dto.getCompanyName());
        //清空授权
        company.setAuthorization("");
        //清空外部系统关联号
        company.setExternalNumber("");
        company.setDelFlag(DelFlagEnum.UNDEL.getNo());
        company.setAddress(dto.getAddress());
        company.setProvince(dto.getProvince());
        company.setCity(dto.getCity());
        company.setZone(dto.getZone());
        company.setEnableWeappMsg(dto.getEnableWeappMsg());
        if (dto.getCommission()) {
            company.setCommissionFlag(YesOrNoEnum.YES.getNo());
        } else {
            company.setCommissionFlag(YesOrNoEnum.NO.getNo());
        }
        if (dto.getWxConfigs() != null && dto.getWxConfigs().size() > 0) {
            company.setAppId(dto.getWxConfigs().get(0).getAppId());
            company.setAppSecret(dto.getWxConfigs().get(0).getAppSecret());
        }
        if (dto.getMessage()) {
            company.setMessageFlag(YesOrNoEnum.YES.getNo());
        } else {
            company.setMessageFlag(YesOrNoEnum.NO.getNo());
        }
        company.setMerchant2(dto.getMerchant2());
        company.setMerchantCode(dto.getMerchantCode());
        company.setOutsideSwitch(dto.getOutsideSwitch());
        company.setCheckSwitch(dto.getCheckSwitch());
        company.setCheckTemp(dto.getCheckTemp());
        company.setIntroduction(dto.getIntroduction());
        company.setCompanyAbbreviation(dto.getCompanyAbbreviation());
        company.setCheckM2Repeat(dto.getCheckM2Repeat());
        company.setEnableWeappMsg(dto.getEnableWeappMsg());
        companyRepository.save(company);
        return company;
    }

    /**
     * 判断域名是否存在
     *
     * @param domain
     * @return
     */
    private boolean checkDomain(String domain) {
        return checkDomain(null, domain);
    }

    private boolean checkDomain(Company company, String domain) {
        if (company != null) {
            if (StringUtil.equals(company.getDomain(), domain)) {
                return true;
            }
        }
        Company example = new Company();
        example.setDomain(domain);
        example.setDelFlag(DelFlagEnum.UNDEL.getNo());
        List<Company> companies = companyRepository.findAll(Example.of(example));
        if (companies != null && companies.size() > 0) {
            if (companies.size() > 1) {
                throw new SystemException(ExceptionEnum.EXIST_MORE_THAN_2_DOMAIN);
            } else {
                if (company != null) {
                    if (companies.get(0).getId().equals(company.getId())) {
                        return true;
                    }
                }
                return false;
            }
        } else {
            return true;
        }
    }

    private void addLogo(Long companyId, String type, String logoUrl) {
        Logo logo = new Logo();
        logo.setLogoMerchantId(companyId);
        logo.setLogoUrl(logoUrl);
        logo.setLogoType(type);
        logo.setDelFlag(DelFlagEnum.UNDEL.getNo());
        logoRepository.save(logo);
    }

    private void addLogo(Long companyId, CompanyDTO dto) {
        if (StringUtil.isNotBlank(dto.getH5LoginLogo())) {
            addLogo(companyId, LogoTypeEnum.TYPE_1.getNo(), dto.getH5LoginLogo());
        }
        if (StringUtil.isNotBlank(dto.getAssistantLoginLogo())) {
            addLogo(companyId, LogoTypeEnum.TYPE_2.getNo(), dto.getAssistantLoginLogo());
        }
        if (StringUtil.isNotBlank(dto.getAssistantIndexLogo())) {
            addLogo(companyId, LogoTypeEnum.TYPE_3.getNo(), dto.getAssistantIndexLogo());
        }
        if (StringUtil.isNotBlank(dto.getAssistantExitLogo())) {
            addLogo(companyId, LogoTypeEnum.TYPE_4.getNo(), dto.getAssistantExitLogo());
        }
        if (StringUtil.isNotBlank(dto.getDevelopersLoginLogo())) {
            addLogo(companyId, LogoTypeEnum.TYPE_5.getNo(), dto.getDevelopersLoginLogo());
        }
        if (StringUtil.isNotBlank(dto.getDevelopersIndexLogo())) {
            addLogo(companyId, LogoTypeEnum.TYPE_6.getNo(), dto.getDevelopersIndexLogo());
        }
        if (StringUtil.isNotBlank(dto.getAgencyLoginLogo())) {
            addLogo(companyId, LogoTypeEnum.TYPE_7.getNo(), dto.getAgencyLoginLogo());
        }
        if (StringUtil.isNotBlank(dto.getAgencyIndexLogo())) {
            addLogo(companyId, LogoTypeEnum.TYPE_8.getNo(), dto.getAgencyIndexLogo());
        }
        if (StringUtil.isNotBlank(dto.getAgencyServiceLogo())) {
            addLogo(companyId, LogoTypeEnum.TYPE_9.getNo(), dto.getAgencyServiceLogo());
        }
        if (StringUtil.isNotBlank(dto.getAgencyWeappLogo())) {
            addLogo(companyId, LogoTypeEnum.TYPE_10.getNo(), dto.getAgencyWeappLogo());
        }
    }

    @Override
    public void updateCompany(CompanyDTO dto) {
        Company company = saveCompany(dto);
        //公司关系 原代码什么都没有做 orz
        //保存微信配置
        wxConfigService.saveWxConfig(company, dto.getWxConfigs());
        //保存logo
        updateLogo(company.getId(), dto);
        //部门
        departmentService.updateTopDepartment(company, null);
        //保存管理员
//        PartnerInfoDTO partnerInfoDTO = dto.getPartnerInfo();
//        partnerInfoDTO.setCompanyId(String.valueOf(company.getId()));
//        partnerInfoDTO.setAccountType(PartnerInfoTypeEnum.ADMIN.getNo());
//        partnerInfoDTO.setResetPassword(false);
//        partnerInfoService.updatePartnerInfo(partnerInfoDTO);
        //操作日志
        operationLogService.addOperationLog(OperationTypeEnum.EDIT.getNo(), company.getId());
    }

    private void updateLogo(Long companyId, CompanyDTO dto) {
        Logo example = new Logo();
        example.setLogoMerchantId(companyId);
        example.setDelFlag(DelFlagEnum.UNDEL.getNo());
        List<Logo> logos = logoRepository.findAll(Example.of(example));
        if (CollectionUtils.isNotEmpty(logos)) {
            Map<String, Logo> logoMap = logos.stream().collect(Collectors.toMap(Logo::getLogoType, Function.identity()));
            if(Objects.nonNull(logoMap.get(LogoTypeEnum.TYPE_1.getNo()))){
                Logo logo = logoMap.get(LogoTypeEnum.TYPE_1.getNo());
                logo.setLogoUrl(dto.getH5LoginLogo());
                logoRepository.save(logo);
            }else{
                if(StringUtil.isNotBlank(dto.getH5LoginLogo())){
                    addLogo(companyId, LogoTypeEnum.TYPE_1.getNo(), dto.getH5LoginLogo());
                }
            }
            if(Objects.nonNull(logoMap.get(LogoTypeEnum.TYPE_2.getNo()))){
                Logo logo = logoMap.get(LogoTypeEnum.TYPE_2.getNo());
                logo.setLogoUrl(dto.getAssistantLoginLogo());
                logoRepository.save(logo);
            }else{
                if(StringUtil.isNotBlank(dto.getAssistantLoginLogo())){
                    addLogo(companyId, LogoTypeEnum.TYPE_2.getNo(), dto.getAssistantLoginLogo());
                }
            }
            if(Objects.nonNull(logoMap.get(LogoTypeEnum.TYPE_3.getNo()))){
                Logo logo = logoMap.get(LogoTypeEnum.TYPE_3.getNo());
                logo.setLogoUrl(dto.getAssistantIndexLogo());
                logoRepository.save(logo);
            }else{
                if(StringUtil.isNotBlank(dto.getAssistantIndexLogo())){
                    addLogo(companyId, LogoTypeEnum.TYPE_3.getNo(), dto.getAssistantIndexLogo());
                }
            }
            if(Objects.nonNull(logoMap.get(LogoTypeEnum.TYPE_4.getNo()))){
                Logo logo = logoMap.get(LogoTypeEnum.TYPE_4.getNo());
                logo.setLogoUrl(dto.getAssistantExitLogo());
                logoRepository.save(logo);
            }else{
                if(StringUtil.isNotBlank(dto.getAssistantExitLogo())){
                    addLogo(companyId, LogoTypeEnum.TYPE_4.getNo(), dto.getAssistantExitLogo());
                }
            }
            if(Objects.nonNull(logoMap.get(LogoTypeEnum.TYPE_5.getNo()))){
                Logo logo = logoMap.get(LogoTypeEnum.TYPE_5.getNo());
                logo.setLogoUrl(dto.getDevelopersLoginLogo());
                logoRepository.save(logo);
            }else{
                if(StringUtil.isNotBlank(dto.getDevelopersLoginLogo())){
                    addLogo(companyId, LogoTypeEnum.TYPE_5.getNo(), dto.getDevelopersLoginLogo());
                }
            }
            if(Objects.nonNull(logoMap.get(LogoTypeEnum.TYPE_6.getNo()))){
                Logo logo = logoMap.get(LogoTypeEnum.TYPE_6.getNo());
                logo.setLogoUrl(dto.getDevelopersIndexLogo());
                logoRepository.save(logo);
            }else{
                if(StringUtil.isNotBlank(dto.getDevelopersIndexLogo())){
                    addLogo(companyId, LogoTypeEnum.TYPE_6.getNo(), dto.getDevelopersIndexLogo());
                }
            }
            if(Objects.nonNull(logoMap.get(LogoTypeEnum.TYPE_7.getNo()))){
                Logo logo = logoMap.get(LogoTypeEnum.TYPE_7.getNo());
                logo.setLogoUrl(dto.getAgencyLoginLogo());
                logoRepository.save(logo);
            }else{
                if(StringUtil.isNotBlank(dto.getAgencyLoginLogo())){
                    addLogo(companyId, LogoTypeEnum.TYPE_7.getNo(), dto.getAgencyLoginLogo());
                }
            }
            if(Objects.nonNull(logoMap.get(LogoTypeEnum.TYPE_8.getNo()))){
                Logo logo = logoMap.get(LogoTypeEnum.TYPE_8.getNo());
                logo.setLogoUrl(dto.getAgencyIndexLogo());
                logoRepository.save(logo);
            }else{
                if(StringUtil.isNotBlank(dto.getAgencyIndexLogo())){
                    addLogo(companyId, LogoTypeEnum.TYPE_8.getNo(), dto.getAgencyIndexLogo());
                }
            }
            if(Objects.nonNull(logoMap.get(LogoTypeEnum.TYPE_9.getNo()))){
                Logo logo = logoMap.get(LogoTypeEnum.TYPE_9.getNo());
                logo.setLogoUrl(dto.getAgencyServiceLogo());
                logoRepository.save(logo);
            }else{
                if(StringUtil.isNotBlank(dto.getAgencyServiceLogo())){
                    addLogo(companyId, LogoTypeEnum.TYPE_9.getNo(), dto.getAgencyServiceLogo());
                }
            }
            if(Objects.nonNull(logoMap.get(LogoTypeEnum.TYPE_10.getNo()))){
                Logo logo = logoMap.get(LogoTypeEnum.TYPE_10.getNo());
                logo.setLogoUrl(dto.getAgencyWeappLogo());
                logoRepository.save(logo);
            }else{
                if(StringUtil.isNotBlank(dto.getAgencyWeappLogo())){
                    addLogo(companyId, LogoTypeEnum.TYPE_10.getNo(), dto.getAgencyWeappLogo());
                }
            }
        }else {
            addLogo(companyId, dto);
        }
    }

    @Override
    public CompanyVO getCompany(Long id) {
        Company company = companyRepository.findById(id)
                .orElseThrow(() -> new BusinessException(ExceptionEnum.NOT_FIND_DB_DATA));
        return convertDomain2VO(company, true);
    }

    @Override
    public ExportResDto getDevPremisesExportByPre(Long devCompanyId) {
        ExportResDto resDto = new ExportResDto();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String date = format.format(new Date());
        List<DevPremisesExport> devPremisesExports = devCompanyExportMapper.getDevPremisesExportByPre(devCompanyId,date);
        resDto.setData(devPremisesExports);
        return resDto;
    }

    @Override
    public ExportResDto getDevPremisesExportByRem(Long devCompanyId) {
        ExportResDto resDto = new ExportResDto();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String date = format.format(new Date());
        List<DevPremisesExport> devPremisesExports = devCompanyExportMapper.getDevPremisesExportByRem(devCompanyId,date);
        resDto.setData(devPremisesExports);
        return resDto;
    }

    @Override
    public void cleanCompanyCache(Long devCompanyId) {
        System.out.println("devCompanyId = " + devCompanyId);
        if(Objects.isNull(devCompanyId)){
            return;
        }
        PartnerInfoDTO partnerInfoDTO = new PartnerInfoDTO();
        partnerInfoDTO.setCompanyId(String.valueOf(devCompanyId));
        List<PartnerInfo> piList = partnerInfoService.selectListByDto(partnerInfoDTO);
        if(CollectionUtils.isEmpty(piList)){
            return;
        }
        // 清理用户权限缓存
        List<Long> partnerIds = piList.stream().map(PartnerInfo::getId).collect(Collectors.toList());
        List<String> deleteKeys = new ArrayList<>();
        if(CollectionUtils.isNotEmpty(partnerIds)){
            for(Long partnerId: partnerIds){
                String userOrgPermissionKey = RedisKeyContant.Permission.DEV_USER_ORG_PERMISSION + partnerId;
                String userPremisesPermissionKey = RedisKeyContant.Permission.DEV_USER_PREMISES_PERMISSION + partnerId;
                deleteKeys.add(userOrgPermissionKey);
                deleteKeys.add(userPremisesPermissionKey);
            }
        }
        RedisUtil.delete(deleteKeys);
        // 清理角色权限缓存
        deleteKeys = new ArrayList<>();
        List<Long> userRoleIds = piList.stream()
                .filter(item -> Objects.nonNull(item.getUserRoleId()))
                .map(PartnerInfo::getUserRoleId).collect(Collectors.toList());
        if(CollectionUtils.isNotEmpty(partnerIds)){
            for(Long userRoleId: userRoleIds){
                String roleOrgPermissionKey = RedisKeyContant.Permission.DEV_ROLE_ORG_PERMISSION + userRoleId;
                String rolePremisesPermissionKey = RedisKeyContant.Permission.DEV_ROLE_PREMISES_PERMISSION + userRoleId;
                deleteKeys.add(roleOrgPermissionKey);
                deleteKeys.add(rolePremisesPermissionKey);
            }
        }
        RedisUtil.delete(deleteKeys);
    }
}
